package com.cli.spring_session_demo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2020/3/25 15:03
 * @Version 1.0
 **/
public class JsonUtils {

    public static String obj2json(Object obj) throws Exception {
        return JSONObject.toJSONString(obj);
    }

    public static <T> T json2obj(String jsonStr, Class<T> clazz) throws Exception {
        return JSON.parseObject(jsonStr,clazz);
    }

    public static <T> Map<String, Object> json2map(String jsonStr)     throws Exception {
        return JSON.parseObject(jsonStr,Map.class);
    }

    public static <T> T map2obj(Map<?, ?> map, Class<T> clazz) throws Exception {
        return JSON.parseObject(obj2json(map),clazz);
    }
}
